package com.zhouchb.stream;

import javax.net.ssl.HttpsURLConnection;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author Zhouchb
 * @Create 2021-11-2021/11/1 :22:36
 * @Description
 */
public class URLTest {
    public static void main(String[] args) {
        InputStream is = null;
        HttpURLConnection urlConnection = null;
        FileOutputStream fos = null;
        try {
            URL url = new URL("http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20190725/6968fcf2ecc74fed86c22020452e738d.jpeg");
             urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            is = urlConnection.getInputStream();
             fos = new FileOutputStream("IO/nezha.jpeg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            System.out.println("下载完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is!=null)
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos!=null)
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (urlConnection!=null)
            urlConnection.disconnect();
        }

    }
}
